# -*- coding:UTF-8 -*-
"""
@author: DadaYu
"""
import os
from datetime import datetime
import requests
from bs4 import BeautifulSoup
import time


# 解析url
def get_beautiful_soup(url):
    res = requests.get(url)
    res.encoding = 'GBK'
    html = res.text
    return BeautifulSoup(html, "html5lib")


def get_list(url, contentUrl):
    bf = get_beautiful_soup(url)
    list = bf.find('ul',class_='mulu_list').find_all('a')
    array = []
    for a in list:
        array.append(contentUrl +a['href'])
    return array


def get_body(file_name, url):
    content = get_beautiful_soup(url).find(id='content')
    title = content.find('h1').text
    append_to_file(file_name, title)
    htmlContent = content.find(id='htmlContent')
    br_tags = htmlContent.find_all("br")
    for br_tag in br_tags:
        br_tag.replace_with("\n")
    cleaned_text = htmlContent.text.replace('\u00A0', ' ')
    append_to_file(file_name, cleaned_text)


def append_to_file(file_name, text):
    # 尝试打开文件，如果文件不存在则创建新文件
    with open(file_name, 'a+',encoding='utf-8') as file:
        # 将文件指针移到文件末尾，以便在原有文本基础上添加内容
        file.seek(0, 2)
        # 写入新的文本内容
        file.write(text + '\n')


if __name__ == '__main__':
    # get_body('https://www.bqg10.com/book/53668/37471593.html')
    host = 'https://www.bqg10.com/'
    contentUrl = host + 'book/53668/'
    url = host + 'book/53668/'
    title = get_beautiful_soup(url).find(id='info').find('h1').text + '.txt'
    list = []
    while True:
        list = list + get_list(url, contentUrl)
        print(f"已获取：{len(list)}章链接", end="\r")
        bf = get_beautiful_soup(url)
        btn = bf.find_all('span', class_='input-group-btn')
        next = btn[1].find('a')
        if next.has_attr('href'):
            href = next['href']
            if href and href != 'javascript:':
                url = host + href
            else:
                break
        else:
            break
    # 获取完成后，延迟一段时间以显示 "已完成"，然后覆盖输出
    time.sleep(1)
    print("\r已完成链接获取" + " " * 20)
    count = len(list)
    n = 1
    for i in list:
        get_body(title, i)
        percentage = (n / count) * 100
        # 输出百分比并更新
        print(f"已下载：{percentage:.0f}%", end="\r")
        n += 1
        time.sleep(1)
    print(f"{title},下载完成")